package vip.idiy.anwei_android_iot.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import vip.idiy.anwei_android_iot.R;

/**
 * ClassName MyImageButton
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 8:53 2023/4/19
 */
public class MyImageButton extends LinearLayout {
    private ImageView imageView;
    private TextView textView;
    private Drawable image;
    private String text;

    public MyImageButton(Context context) {
        super(context);
        init(context, null);
    }

    public MyImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.layout_image_button, this, true);
        imageView = view.findViewById(R.id.layout_image_button_imageview);
        textView = view.findViewById(R.id.layout_image_button_textview);
        @SuppressLint("CustomViewStyleable") TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageButton);
        image = a.getDrawable(R.styleable.ImageButton_image);
        if (image != null) {
            imageView.setImageDrawable(image);
        }
        text = a.getString(R.styleable.ImageButton_text);
        if (text != null) {
            textView.setText(text);
        }
        int color = a.getColor(R.styleable.ImageButton_color, 0);
        if (text != null) {
            textView.setTextColor(color);
        }
        a.recycle();
    }

    /**
     * 设置图片
     * @param resId
     */
    public void setImageResource(int resId) {
        image = ResourcesCompat.getDrawable(getResources(), resId, null);
        imageView.setImageDrawable(image);
    }


    /**
     * 设置按钮文本
     * @param text
     */
    public void setText(String text) {
        this.text = text;
        textView.setText(text);
    }

    /**
     * 设置文本颜色
     * @param color
     */
    public void setTextColor(int color) {
        textView.setTextColor(color);
    }
}
